package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.RoutinePathologySampleMapper;
import com.zhijian.medical.entity.po.RoutinePathologySample;
import com.zhijian.medical.entity.vo.request.RoutinePathologySampleSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.RoutinePathologySampleSearchVo;
import com.zhijian.medical.entity.vo.response.RoutinePathologySampleVo;
import com.zhijian.medical.enums.RoutinePathologySampleStepEnum;
import com.zhijian.medical.service.RoutinePathologySampleService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病理样本业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-06-10 13:27:28
 */
@Slf4j
@Service
public class RoutinePathologySampleServiceImpl implements RoutinePathologySampleService {

    @Autowired
    private RoutinePathologySampleMapper mapper;

    @Override
    public RoutinePathologySample selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutinePathologySampleVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public RoutinePathologySampleVo selectVoByConventionPathologyNumber(String conventionPathologyNumber) {
        return mapper.selectVoByConventionPathologyNumber(conventionPathologyNumber);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutinePathologySample routinePathologySample) {
        Date now = new Date();
        routinePathologySample.setCreateTime(now);
        routinePathologySample.setUpdateTime(now);
        return mapper.insert(routinePathologySample) > 0 ? routinePathologySample.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutinePathologySample routinePathologySample) {
        Date now = new Date();
        routinePathologySample.setCreateTime(now);
        routinePathologySample.setUpdateTime(now);
        return mapper.insertSelective(routinePathologySample) > 0 ? routinePathologySample.getId() : 0L;
    }

    @Override
    public boolean update(RoutinePathologySample routinePathologySample) {
        Date now = new Date();
        routinePathologySample.setUpdateTime(now);
        return mapper.updateByPrimaryKey(routinePathologySample) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutinePathologySample record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutinePathologySampleSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologySample routinePathologySample = new RoutinePathologySample();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologySample);
        routinePathologySample.setStep(RoutinePathologySampleStepEnum.QC.getCode().byteValue());
        return this.insert(routinePathologySample) != 0;
    }

    @Override
    public boolean modify(RoutinePathologySampleSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologySample old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutinePathologySample routinePathologySample = new RoutinePathologySample();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologySample);
        return this.update(routinePathologySample);
    }

    @Override
    public RoutinePathologySampleVo selectOne(RoutinePathologySampleSearchVo routinePathologySampleSearchVo) {
        return mapper.selectOne(routinePathologySampleSearchVo);
    }

    @Override
    public Integer count(RoutinePathologySampleSearchVo routinePathologySampleSearchVo) {
        return mapper.count(routinePathologySampleSearchVo);
    }

    @Override
    public List<RoutinePathologySampleVo> selectList(RoutinePathologySampleSearchVo routinePathologySampleSearchVo) {
        return mapper.selectList(routinePathologySampleSearchVo);
    }

    @Override
    public Page<RoutinePathologySampleVo> selectPage(RoutinePathologySampleSearchVo routinePathologySampleSearchVo) {
        AssertUtil.check(Objects.isNull(routinePathologySampleSearchVo.getPageNum()) || Objects.isNull(routinePathologySampleSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routinePathologySampleSearchVo);
        //分页
        Page<RoutinePathologySampleVo> page = new Page<>(true, count, routinePathologySampleSearchVo.getPageNum(), routinePathologySampleSearchVo.getPageSize());
        routinePathologySampleSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutinePathologySampleVo> responseVos = this.selectList(routinePathologySampleSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutinePathologySample> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutinePathologySampleSearchVo routinePathologySampleSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routinePathologySampleSearchVo);
    }

}